package com.net.wanjian.networkhospitalmanager.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.net.wanjian.networkhospitalmanager.R;
import com.net.wanjian.networkhospitalmanager.adapter.base.RecyclerBaseAdapter;
import com.net.wanjian.networkhospitalmanager.bean.evaluate.SearchTeacherEvaluationAllInfoListResult;
import com.net.wanjian.networkhospitalmanager.utils.SharedXmlUtil;
import com.net.wanjian.networkhospitalmanager.utils.URLDecoderUtil;
import com.net.wanjian.networkhospitalmanager.view.LRatingBar;
import com.zhy.autolayout.utils.AutoUtils;

import java.text.DecimalFormat;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Description:
 * Created by lidongcai on 17/7/20.
 * Email:  lidongcaiocc@163.com
 */

public class EvaluateDetailsTeacherContentAdapter extends RecyclerBaseAdapter<SearchTeacherEvaluationAllInfoListResult.DataBean.EvaluationTableListBean, EvaluateDetailsTeacherContentAdapter.ViewHolder> {

    public EvaluateDetailsTeacherContentAdapter(Context context) {
        super(context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_evaluate_content_list, parent, false);
        AutoUtils.auto(view);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, SearchTeacherEvaluationAllInfoListResult.DataBean.EvaluationTableListBean item, int position) {

       /* //名字只取出第一个字符
        String name = URLDecoderUtil.getDecoder(item.getEvaluatorUserName());
        //保留姓氏
        char str2 = name.charAt(0);
        String str1 = name.substring(1);
        //用正则表达式替换(包括汉字，数字，大小写字母)
        str1 = str1.replaceAll("[^x00-xff]|\\w", "*");
        holder.itemEvaluateContentNameTv.setText(str2+str1);*/

       //评论为本人显示姓名
        holder.itemEvaluateContentNameTv.setText("匿名评价");
        if (URLDecoderUtil.getDecoder(item.getUserIdentityID()).equals(SharedXmlUtil.getInstance().getUserIdentityId())){
            holder.itemEvaluateContentNameTv.setText(URLDecoderUtil.getDecoder(item.getEvaluatorUserName()));
        }
        String desc = context.getResources().getString(R.string.no_event_evaluate);
        if (!URLDecoderUtil.getDecoder(item.getEvaluationTextContentInfo()).equals("")){
            desc = URLDecoderUtil.getDecoder(item.getEvaluationTextContentInfo());
        }
        holder.itemEvaluateContentDescTv.setText(desc);
        holder.itemEvaluateContentDateTv.setText(URLDecoderUtil.getDecoder(item.getEvaluationItemValueTime()));


        String starNum = "0";
        DecimalFormat starFormat = new DecimalFormat("#");
        String format = "0";
        DecimalFormat decimalFormat = new DecimalFormat("#.0");
        float valueResult = Float.parseFloat(item.getEvaluationTableAverageValueResult());
        if (valueResult != 0) {
            format = decimalFormat.format(valueResult);
            starNum = starFormat.format(valueResult);
        }
        //设置星星和值
        holder.itemEvaluateContentScoreTv.setText(format);
        holder.itemEvaluateDetailsTeacherStarRatingbar.setStar(Float.parseFloat(starNum));


        final RecyclerView recyclerView = holder.itemEvaluateContentStarRecycler;
        recyclerView.setLayoutManager(new LinearLayoutManager(context));

        EvaluateContentStarAdapter starAdapter = new EvaluateContentStarAdapter(context);
        recyclerView.setAdapter(starAdapter);
        starAdapter.setList(item.getEvaluationItemList());


        holder.itemEvaluateContentStarIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (recyclerView.getVisibility() == View.VISIBLE) {
                    recyclerView.setVisibility(View.GONE);
                } else {
                    recyclerView.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.item_evaluate_content_name_tv)
        TextView itemEvaluateContentNameTv;
        @BindView(R.id.item_evaluate_details_teacher_star_ratingbar)
        LRatingBar itemEvaluateDetailsTeacherStarRatingbar;
        @BindView(R.id.item_evaluate_content_score_tv)
        TextView itemEvaluateContentScoreTv;
        @BindView(R.id.item_evaluate_content_date_tv)
        TextView itemEvaluateContentDateTv;
        @BindView(R.id.item_evaluate_content_desc_tv)
        TextView itemEvaluateContentDescTv;
        @BindView(R.id.item_evaluate_content_star_iv)
        ImageView itemEvaluateContentStarIv;
        @BindView(R.id.item_evaluate_content_star_recycler)
        RecyclerView itemEvaluateContentStarRecycler;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
